import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'

// 课程和学期数据（可改为接口获取）
const courseList = ['语文', '数学', '英语']
const termList = ['2025春季学期', '2025秋季学期']
const tableData = [
  // 张三
  {
    id: 1,
    studentId: '202501',
    name: '张三',
    course: '语文',
    score: '85',
    term: '2025春季学期',
    createBy: '王老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 2,
    studentId: '202501',
    name: '张三',
    course: '数学',
    score: '90',
    term: '2025春季学期',
    createBy: '李老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 3,
    studentId: '202501',
    name: '张三',
    course: '英语',
    score: '78',
    term: '2025春季学期',
    createBy: '赵老师',
    createTime: '2025-03-10 12:00:00',
  },

  // 李四
  {
    id: 4,
    studentId: '202502',
    name: '李四',
    course: '语文',
    score: '92',
    term: '2025春季学期',
    createBy: '王老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 5,
    studentId: '202502',
    name: '李四',
    course: '数学',
    score: '88',
    term: '2025春季学期',
    createBy: '李老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 6,
    studentId: '202502',
    name: '李四',
    course: '英语',
    score: '81',
    term: '2025春季学期',
    createBy: '赵老师',
    createTime: '2025-03-10 12:00:00',
  },

  // 王五
  {
    id: 7,
    studentId: '202503',
    name: '王五',
    course: '语文',
    score: '76',
    term: '2025春季学期',
    createBy: '王老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 8,
    studentId: '202503',
    name: '王五',
    course: '数学',
    score: '84',
    term: '2025春季学期',
    createBy: '李老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 9,
    studentId: '202503',
    name: '王五',
    course: '英语',
    score: '89',
    term: '2025春季学期',
    createBy: '赵老师',
    createTime: '2025-03-10 12:00:00',
  },

  // 赵六
  {
    id: 10,
    studentId: '202504',
    name: '赵六',
    course: '语文',
    score: '80',
    term: '2025春季学期',
    createBy: '王老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 11,
    studentId: '202504',
    name: '赵六',
    course: '数学',
    score: '73',
    term: '2025春季学期',
    createBy: '李老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 12,
    studentId: '202504',
    name: '赵六',
    course: '英语',
    score: '86',
    term: '2025春季学期',
    createBy: '赵老师',
    createTime: '2025-03-10 12:00:00',
  },

  // 钱七
  {
    id: 13,
    studentId: '202505',
    name: '钱七',
    course: '语文',
    score: '95',
    term: '2025春季学期',
    createBy: '王老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 14,
    studentId: '202505',
    name: '钱七',
    course: '数学',
    score: '89',
    term: '2025春季学期',
    createBy: '李老师',
    createTime: '2025-03-10 12:00:00',
  },
  {
    id: 15,
    studentId: '202505',
    name: '钱七',
    course: '英语',
    score: '91',
    term: '2025春季学期',
    createBy: '赵老师',
    createTime: '2025-03-10 12:00:00',
  },
]

const useScoreStore = defineStore('Score', {
  state: () => {
    return {
      courseList,
      termList,
      allData: tableData,
      tableData: tableData,
      queryForm: {
        studentId: '',
        name: '',
        course: '',
      },
    }
  },
  actions: {
    // 查询方法
    queryData() {
      this.tableData = this.allData.filter((item) => {
        return (
          (!this.queryForm.studentId ||
            item.studentId.includes(this.queryForm.studentId)) &&
          (!this.queryForm.name || item.name.includes(this.queryForm.name)) &&
          (!this.queryForm.course ||
            item.course.includes(this.queryForm.course))
        )
      })
      if (this.tableData.length) {
        ElMessage({
          showClose: true,
          message: '为你找到以下数据',
          type: 'success',
        })
        console.log(this.tableData.length)
      } else {
        ElMessage({
          showClose: true,
          message: '找不到数据，请检查输入',
          type: 'error',
        })
      }
    },
    // 重置方法
    reset() {
      this.tableData = this.allData
      this.queryForm = { studentId: '', name: '', course: '' }
      ElMessage.success('重置成功！')
    },
    // 添加成绩方法
    addScore(score: any) {
      // id 用时间戳模拟唯一性
      score.id = Date.now()
      this.tableData.unshift(score)
    },
    // 编辑成绩方法
    updateScore(score: any) {
      const index = this.tableData.findIndex((item) => item.id === score.id)
      if (index !== -1) {
        this.tableData[index] = { ...score }
      }
    },
    // 单条删除
    remove(id: any) {
      this.tableData = this.tableData.filter((item) => item.id !== id)
      this.allData = this.allData.filter((item) => item.id !== id)
    },
    // 批量删除
    removeBatch(ids: any) {
      this.tableData = this.tableData.filter((item) => !ids.includes(item.id))
      this.allData = this.allData.filter((item) => !ids.includes(item.id))
    },
  },
})

export default useScoreStore
